package com.huawei.eidc.message.service.impl;

import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import com.huawei.eidc.message.EncodeUtil;
import com.huawei.eidc.message.Sample;
import com.huawei.eidc.message.service.WSDService;

public class WSDServiceImpl implements WSDService {

	@Override
	public Document getReqXmlTemplate(String fileName) {
		
		File file = new File(reqXmlFilePath + fileName + ".xml");
    	SAXReader reader = new SAXReader();
    	Document document = null;
    	try {
    		document = reader.read(file);
    		System.out.println("模版文件：" + getXMLStr(document));
		} catch (Exception e) {
			System.out.println("模版文件读取失败");
		}
		return document;
	}

	@Override
	public String requestSend(String reqXml, String method) {
		String response = ""; 
	    try {
            Service service = new org.apache.axis.client.Service();
            org.apache.axis.client.Call call = null;
            call = (org.apache.axis.client.Call) service.createCall();
            call.setOperationName(new javax.xml.namespace.QName($wsdl_url, method));
            call.setTargetEndpointAddress(new java.net.URL($wsdl_url));
            String ret = (String) call.invoke(new Object[] {reqXml});
            Document document = DocumentHelper.parseText(ret);
            String strBody = document.getRootElement().element("BODY").getTextTrim();
         	response = EncodeUtil.decrypt(strBody,TEST_KEY);
         	System.out.println("response:"+response);
	    }catch (Exception e) {
			e.printStackTrace();
		}
	    
		return response;
	}

	@Override
	public String getXMLStr(Document document) {
		 try
	        {
	            java.io.StringWriter sw = new java.io.StringWriter();
	            OutputFormat format = OutputFormat.createCompactFormat();
	            format.setEncoding("UTF8");
	            org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(sw,format);
	            xmlWriter.write(document);
	            xmlWriter.close();
	            return sw.getBuffer().toString();
	        }
	        catch(Exception ex)
	        {
	            ex.printStackTrace();
	            return "";
	        }
	}

	@Override
	public String setBody(Map<String, String> map) {
		Document document = DocumentHelper.createDocument();
		Element body = document.addElement("BODY");
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		String key = "";
		while (iterator.hasNext()) {
			key = (String)iterator.next();
			body.addElement(key).setText(map.get(key));
		}
		String result = EncodeUtil.encrypt(getXMLStr(document), TEST_KEY);
		return result;
	}

	@Override
	public Element setElement(Element element, Map<String, String> map) {
		
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		String key = "";
		while (iterator.hasNext()) {
			key = (String)iterator.next();
			element.addElement(key).setText(map.get(key));
		}
		return element;
	}

}
